package com.epfresh.api.utils;

import android.content.Context;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.epfresh.api.R;
/**
 * Created by wzh on 2016/4/26.
 */
public class T {

    public static Context context;

    private static Toast toast = null;

    public T() {
        super();
    }

    public static void init(Context context) {
        T.context = context;
    }

    public static void toast(String content) {
        if (context == null) {
            return;
        }
        //if (toast == null) {
        cancel();
            toast = Toast.makeText(context, content + "", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        /*} else {
            toast.setText(content + "");
        }*/
        // 显示消息
        toast.show();
    }

    public static void cancel() {
        if (toast != null)
            toast.cancel();
    }



    /**
     * 自定义背景的消息提示
     *
     * @param context
     */
    public static void offerImageToast(Context context, int top,int left,
                                       String str,boolean leftFlag) {
        // 创建一个Toast提示消息
/*		toast = Toast.makeText(context, str, 0);
		// 设置Toast提示消息在屏幕上的位置
		toast.setGravity(Gravity.CENTER, 0, 0);
		// 获取Toast提示消息里原有的View
		View toastView = toast.getView();
		// 创建一个ImageView
		ImageView img = new ImageView(context);
		img.setImageResource(ImageResourceId);
		// 创建一个LineLayout容器
		LinearLayout ll = new LinearLayout(context);
		// 向LinearLayout中添加ImageView和Toast原有的View
		ll.setGravity(Gravity.CENTER_VERTICAL);
		ll.setOrientation(LinearLayout.VERTICAL);
		ll.addView(img);
		ll.addView(toastView);
		// 将LineLayout容器设置为toast的View
		toast.setView(ll);*/
        cancel();

        toast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
        LinearLayout toastView = (LinearLayout) toast.getView();
        if(leftFlag) {
            toast.setGravity(Gravity.TOP,  LocalDisplay.dp2px(12),top-LocalDisplay.dp2px(35));
            toastView.setPadding(LocalDisplay.dp2px(30), LocalDisplay.dp2px(10), LocalDisplay.dp2px(20), LocalDisplay.dp2px(10));
            toastView.setBackgroundResource(R.mipmap.home_often_tip_left);
        }else{
            toast.setGravity(Gravity.TOP,  -LocalDisplay.dp2px(50),top-LocalDisplay.dp2px(25));
            toastView.setPadding(LocalDisplay.dp2px(20), LocalDisplay.dp2px(20), LocalDisplay.dp2px(30), LocalDisplay.dp2px(10));
            toastView.setBackgroundResource(R.mipmap.home_often_tip_right);
        }
        toast.show();
        // 显示消息
    }
}
